﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kooboo.HtmlFunc;
using Kooboo.HtmlFunc.Providers;

namespace Kooboo.CMS.SiteConverter.Parser.NVelocity
{
    public class LabelParser : ParserBase, IParser<ILabel>
    {
        private static string DoubleQuotesEncode(string input)
        {
            if (string.IsNullOrEmpty(input)) { return input; }
            else { return input.Replace("\"", "'").Replace(Environment.NewLine, string.Empty); }
        }

        private static string GetInnerText(ILabel label)
        {
            var innerHtml = NodeExtension.GetInnerContent(label.StartNode, label.EndNode);
            return DoubleQuotesEncode(innerHtml);
        }

        #region IParser<ILabel> Members

        public void Parse(ILabel obj)
        {
            string markup = null;
            if (string.IsNullOrEmpty(obj.Key))
            {
                markup = string.Format("$ViewHelper.Label(\"{0}\")", GetInnerText(obj));
            }
            else
            {
                markup = string.Format("$ViewHelper.Label(\"{0}\", \"{1}\")", GetInnerText(obj), DoubleQuotesEncode(obj.Key));
            }
            var markupNode = NodeExtension.CreateNode(markup);
            Razor.LabelParser.ReplateInner(obj, markupNode);
        }

        #endregion
    }
}
